package com.yl.navigation.ui

import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayoutMediator
import com.yl.common.base.BaseVMFragment
import com.yl.navigation.R
import com.yl.navigation.adapter.NavigationAdapter
import com.yl.navigation.databinding.FragmentNavigationBinding
import com.yl.navigation.viewmodel.TreeViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel

class NavigationFragment : BaseVMFragment<FragmentNavigationBinding>() {

    private lateinit var titleList: ArrayList<String>
    private lateinit var fragmentList: ArrayList<Fragment>
    private lateinit var navigationAdapter: NavigationAdapter

    override fun getLayoutID() = R.layout.fragment_navigation


    override fun observe() {
    }

    override fun init() {
        titleList = arrayListOf("体系", "系统")
        fragmentList = arrayListOf(TreeFragment(), SystemFragment())

        navigationAdapter = NavigationAdapter(this, fragmentList)
        mBind.viewPager.adapter = navigationAdapter
        TabLayoutMediator(mBind.tabLayout, mBind.viewPager) { tab, position ->
            tab.text = titleList[position]
        }.attach()

        mBind.viewPager.isSaveEnabled = false
    }

    fun scrollToTop() {
        if (navigationAdapter.itemCount == 0) {
            (navigationAdapter.createFragment(0) as TreeFragment).scrollToTop()
        } else if (mBind.viewPager.currentItem == 1) {
            (navigationAdapter.createFragment(1) as SystemFragment).scrollToTop()
        }
    }
}

